/* eslint-disable object-curly-spacing */
/* eslint-disable object-curly-spacing */
<template>
    <div class="login-container">
        <div class="login-form-wrap">
            <!-- logo区域 -->
            <div class="login-head">
                <div class="logo"></div>
            </div>
            <!-- 表单区域 -->
            <el-form ref="form" class="login-form" :rules="rules" :model="user">
                <el-form-item prop="mobile" >
                    <el-input v-model="user.mobile"></el-input>
                </el-form-item>

                <el-form-item prop="code" >
                    <el-input v-model="user.code" show-password></el-input>
                </el-form-item>

                <el-form-item prop="isAgree">
                    <el-checkbox v-model="user.isAgree" label="我已阅读并同意用户协议和隐私条款" name="type"></el-checkbox>
                </el-form-item>
                <el-button class="login-btn" @click="hLogin" >登录</el-button>
            </el-form>
        </div>
    </div>
</template>

<script>
// import axios from '../utils/request.js'
export default {
  data () {
    return {
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入合法的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入合法的验证码', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: function (rule, value, callback) {
              console.log(rule, value, callback)
              if (value) {
                callback()
              } else {
                callback(new Error('你必须要先同意'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      user: {
        mobile: '13911111111',
        code: '246810',
        isAgree: true
      }
    }
  },
  methods: {
    doLogin () {
      const { mobile, code } = this.user
      this.$axios({
        method: 'POST',
        url: '/mp/v1_0/authorizations',
        data: {
          // eslint-disable-next-line no-undef
          mobile,
          // eslint-disable-next-line no-undef
          code
        }
      }).then(res => {
        this.$message({
          message: '恭喜你，登录成功',
          type: 'success'
        })
        this.isLoading = false
        // 这里要设置token, 不然跳不进去的
        localStorage.setItem('tokenStr', res.data.data.token)
        this.$router.push('/')
      // eslint-disable-next-line handle-callback-err
      }).catch(err => {
        this.$message({
          message: '登录失败',
          type: 'error'
        })
        this.isLoading = false
      })
    },
    hLogin () {
      this.$refs.form.validate(valid => {
        // console.log(valid)
        if (valid) {
          this.doLogin()
        }
      })
      this.isLoading = true
    }
  }
}
</script>
<style lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
        background-color: skyblue;
        color:#fff;
      }
    }
  }
}
</style>
